<?=  $this->jqgrid ?>
<script type="text/javascript">
$(document).ready(function() {
    var tableId = "#<?= $this->jqgrid->tableId ?>";
	$(tableId).jqGrid('navButtonAdd', "#<?= $this->jqgrid->pagerId ?>",{
		caption: "",
        position: "first",
		buttonicon: "ui-icon-pencil",
		title: "Редактирование профиля пользователя",
		onClickButton : function () {
            var rowId = $(tableId).jqGrid('getGridParam', 'selrow');
            if (typeof rowId == 'string') {
                rowId = parseInt( rowId.replace(/^[^\d]+/, "") );
            }

            if (rowId > 0) {
                var url = <?= Zend_Json::encode((string)$this->editUserUrl); ?>;
                if (url.indexOf("?") != -1) {
                    url += "&id=" + rowId;
                } else {
                    url += "?id=" + rowId;
                }
                window.location.href = url;
            } else {
                info_dialog("Внимание", "Пожалуйста, выберите пользователя", null, {width:"226px"});
            }

		}
	});

    $("#<?= $this->jqgrid->tableId ?>").jqGrid('navButtonAdd', "#<?= $this->jqgrid->pagerId ?>",{
		caption: "",
        position: "first",
		buttonicon: "ui-icon-plus",
		title: "Добавление нового пользователя",
		onClickButton : function () {
			window.location.href=<?= Zend_Json::encode((string)$this->addUserUrl); ?>;
		}
	});

	/*$("#<?= $this->jqgrid->tableId ?>").jqGrid('navButtonAdd', "#<?= $this->jqgrid->pagerId ?>",{
		caption: "",// "Столбцы",
		title: "Выбор и перемещение столбцов таблицы",
		onClickButton : function () {
			$("#<?= $this->jqgrid->tableId ?>").jqGrid('columnChooser');
		}
	});*/
});

var <?= $this->jsObject ?> = {

	opt: <?= $this->jqgrid->invokeParams ?>
	
};

(function ($, $f) {

	$.extend($f, {

		columnChooser: function() {
			$("#<?= $this->jqgrid->tableId ?>").jqGrid('columnChooser');
		},

		changeGroup: function() {
			var rowId = jQuery("#<?= $this->jqgrid->tableId ?>").jqGrid("getGridParam", "selrow");
			if (rowId) {
				rowId = rowId.replace(/\D/g,'');
			}
			
			if (rowId > 0) {
				url = $.exReqPath($f.opt.groupUrl) + "?" + $.param($.extend($.exReqQuery($f.opt.groupUrl), {id: rowId}));
				location.href = url;
			} else {
				info_dialog("Внимание", "Пожалуйста, выберите пользователя", null, {width:"226px"});
			}
		},

		getUserListAjaxForm: function(subgrid_id, row_id) {
			$.ajax({
				type: 'get',
				url: '/users/admin/profile',
				data: ({'id': row_id.replace(/\D/g,'') }),
				dataType: 'html',
				success: function(server) {
					var options = {
						url: '/users/admin/profile?id='+ row_id.replace(/\D/g,''),
						target: $('#'+subgrid_id),
						type: 'POST',
						beforeSubmit: function(arr, $form, options) {
							$form.attr('disabled','disabled');
						},
						success: function(response,status,request) {
							$('#'+subgrid_id).html(response);
							var form = $('#'+subgrid_id+" form");
							form.ajaxForm(options);

							if (request.status == 200) {
								$("#<?= $this->jqgrid->tableId ?>").trigger('reloadGrid');
							}
							else {
								$(":submit",form).button({});
							}
						}
					};

					$('#'+subgrid_id).html(server);
					var form = $('#'+subgrid_id+" form");
					form.ajaxForm(options);
					$(":submit",form).button({});
				}
			});			
		}

	});

})(jQuery, <?= $this->jsObject ?>);

</script>
